package com.chak.voyage.crawler.processor.lvmama.schedual;

import com.chak.voyage.crawler.processor.lvmama.LvmamaProcessor;
import com.chak.voyage.data.entity.TDuplicate;
import com.chak.voyage.data.entity.TNote;
import com.chak.voyage.service.DataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.scheduler.PriorityScheduler;

import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Created by delzh on 2016/9/8.
 */
@Component
public class LvmamaScheduler extends PriorityScheduler {

    @Autowired
    private DataService dataService;

    private static boolean inited = false;

    private static Logger LOG = LoggerFactory.getLogger(LvmamaScheduler.class);

    CopyOnWriteArraySet<String> urlSet = new CopyOnWriteArraySet<String>();

    public void init(){
        LOG.info("开始初始化Scheduler...");
        List<TDuplicate> duplicates = dataService.getAllDuplicate(3);
        for (TDuplicate duplicate : duplicates){
            urlSet.add(duplicate.getUrl());
        }
        LOG.info("初始化Scheduler成功!");
        inited = true;
    }

    public void push(Request request, Task task) {
        if (!inited){
            init();
        }
        if (urlSet.add(request.getUrl())) {
            dataService.addDuplicate(request.getUrl(), (Integer) request.getExtra("type"));
            super.push(request, task);
        }
    }
}
